package web.servlet;

import domain.Cart;
import domain.LineItem;
import domain.Order;
import service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

public class ViewOrderFormServlet extends HttpServlet {

    private static final String VIEW_ORDER_FORM = "/WEB-INF/jsp/order/viewOrder.jsp";

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderService orderService = new OrderService();
        String type = req.getParameter("type");
        if(type.equals("insert")){
            //将订单信息加入数据库表单中
            HttpSession session = req.getSession();
            Order order = (Order) session.getAttribute("order");//成功取得order
            session.setAttribute("lineItems",order.getLineItems());
            orderService.insertOrder(order);
            //购物车清空
            session.setAttribute("cart",null);
        }else {
            int orderId = Integer.parseInt(req.getParameter("orderId"));
            Order order = orderService.getOrder(orderId);
            HttpSession session = req.getSession();
            session.setAttribute("order",order);
        }
        req.getRequestDispatcher(VIEW_ORDER_FORM).forward(req,resp);
    }
}
